﻿using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data.Linq;

namespace Estimate
{
    public enum ConnectionStates { Open, Close };
    public enum UserRules { Admin, Trader };


    public class Root
    {
        public Root()
        {
            this.connectionState = ConnectionStates.Close;
        }

        EstimateDataContext context;
        public EstimateDataContext Context
        {
            get { return context; }
            set { context = value; }
        }


        string connectionString;
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; context = new EstimateDataContext(connectionString); }
        }

        ConnectionStates connectionState;
        public ConnectionStates ConnectionState
        {
            get { return connectionState; }
            set { connectionState = value; }
        }

        UserRules userRule;
        public UserRules UserRule
        {
            get { return userRule; }
            set { userRule = value; }
        }

        Form mDIParent;
        public Form MDIParent
        {
            get { return mDIParent; }
            set { mDIParent = value; }
        }


        public void ShowForm(FormMDIChildTemplate form)
        {
            form.MdiParent = mDIParent;
            form.root = this;
            //this.context = new EstimateDataContext(connectionString);
            form.context = this.context;
            form.db = new Db(context);
            form.Icon = mDIParent.Icon;
            form.Show();
        }

        internal void TranslateGrid(DataGridView grid)
        {
            FieldDescription fieldDescription = new FieldDescription();
            foreach (DataGridViewColumn column in grid.Columns)
                column.HeaderText = fieldDescription.GetDescription(column.HeaderText);

        }

        public bool Submit(bool showMessage)
        {
            try
            {
                context.SubmitChanges();
                if (showMessage) formMain.ShowInformation("Зміни Внесено!");
                return true;
            }

            catch(Exception e)
            {
                formMain.ShowError("Помилка при Внесенні Змін!" + e.Message);
                //context = null;
                //context = new EstimateDataContext(connectionString);
                RollbackChanges(context);
                return false;
            }
        }

        public static void RollbackChanges(EstimateDataContext dataContext)
        {
            ChangeSet changeSet = dataContext.GetChangeSet();
            List<object> inserts = new List<object>(changeSet.Inserts);
            List<object> deleteAndUpdate = new List<object>(changeSet.Deletes);
            deleteAndUpdate.AddRange(changeSet.Updates);

            foreach (object inserted in inserts)
            {
                ITable table = dataContext.GetTable(inserted.GetType());
                table.DeleteOnSubmit(inserted);
            }

            dataContext.Refresh(RefreshMode.OverwriteCurrentValues, deleteAndUpdate.ToArray());
        }

        public bool Submit()
        { return Submit(false); }
    }
}
   
    
      
